package com.palmelf.util;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

public abstract class BaseMonitorThread extends Thread {
	private static Logger LOG = Logger.getLogger("StartLoad");
	public final static String imgFolder = "carMonitor";
	// public final static String imgPrefix = "pm";
	public final static String IMG_PREFIX = "pm";
	// 文件类型
	public List<String> fileType = new ArrayList<String>();
	// 被监控的目录
	protected String monitorPath = null;
	// 将文件复制到这个目录
	protected String targetPath = "";
	protected String appPath;
	// 用于内部控制只有一个线程执行扫描
	protected boolean runable = true;
	// 用于外部控制线程安全退出的,[如果正在执行,则让该周期继续执行下去,否则给退出]
	volatile boolean stop = false;
	// ======================================================
	// 图片的宽度超过展示屏后不显示
	public final static int TERMINAL_BROWSER_WIDTH = 1270;
	public final static int TERMINAL_BROWSER_HEIGHT = 760;
	// 水印离图片右边边缘的距离
	public final static int BACKGROUND_MARGIN_RIGHT = 20;
	// 水印离图片上边边缘的距离
	public final static int BACKGROUND_MARGIN_TOP = 20;
	public final static int BACKGROUND_WIDTH = 3;
	public final static int BACKGROUND_HEIGHT = 6;

	// ======================================================

	public abstract String getImgprefix();

	public void setAppPath(String appPath) {
		this.appPath = appPath;
	}

	@Override
	public void run() {
		Date d = new Date();
		LOG.debug("当前时间" + d.getHours());
		// 在8:00点和18:59之间执行
		runable = d.getHours() >= 7 && d.getHours() <= 22;
		LOG.debug("图片扫描线程条件....内部条件runable:" + runable + "外部条件stop:" + stop);
		while (true == runable && false == stop) {
			if (true == runable) {
				runable = false;
				executeRun();
				runable = true;
			}
			try {
				Thread.sleep(2 * 1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		LOG.debug("图片扫描线程退出....");
	}

	protected abstract void executeRun();

	/**
	 * 生成目标文件名
	 * 
	 * @return
	 */
	protected String getTargetFileName() {
		StringBuffer imgPath = new StringBuffer();
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
		imgPath.append(getImgprefix() + "_");
		imgPath.append(sdf.format(new Date()));
		imgPath.append("_");
		imgPath.append(System.currentTimeMillis());
		imgPath.append(".jpg");
		return imgPath.toString();
	}

	public void setFileType(List<String> fileType) {
		List<String> tempFileType = new ArrayList<String>();
		for (String string : fileType) {
			tempFileType.add(string.toLowerCase());
		}
		this.fileType = tempFileType;
	}

	public String getTargetPath() {
		return targetPath;
	}

	public void setTargetPath(String targetPath) {
		this.targetPath = targetPath;
	}

	public void setMonitorPath(String monitorPath) {
		this.monitorPath = monitorPath;
	}
}
